function difftime(value) {
    let time = null;
    if (typeof (value) === 'string') {
        time = new Date(value).getTime();
    } else {
        time = value;
    }
    const nowTime = new Date().getTime();
    const difftime = nowTime - time;
    if (difftime / 1000 / 3600 / 24 / 30 / 12 >= 1) {
        const d = parseInt(difftime / 1000 / 3600 / 24 / 30 / 12);
        return `${d}年前`;
    } else if (difftime / 1000 / 3600 / 24 / 30 >= 1) {
        const d = parseInt(difftime / 1000 / 3600 / 24 / 30);
        return `${d}月前`;
    } else if (difftime / 1000 / 3600 / 24 >= 1) {
        const d = parseInt(difftime / 1000 / 3600 / 24);
        return `${d}天前`;
    } else if (difftime / 1000 / 3600 >= 1) {
        const h = parseInt(difftime / 1000 / 3600);
        return `${h}小时前`;
    } else if (difftime / 1000 / 60 >= 1) {
        const m = parseInt(difftime / 1000 / 60);
        return `${m}分钟前`;
    } else {
        const s = parseInt(difftime / 1000);
        return s > 10 ? `${s}秒前` : '刚刚';
    }
}

function chatTime(value) {
    const nowTime = new Date(new Date().toLocaleDateString()).getTime();
    const newTime = typeof (value) === 'string' ? new Date(value).getTime() : value;
    if (nowTime > newTime) {
        const difftime = nowTime - newTime;
        if (difftime / 1000 / 3600 / 24 >= 1) {
            return `${leftPad(new Date(newTime).getMonth() + 1)}月${leftPad(new Date(newTime).getDate())}日 ${getTime(newTime)}`;
        } else {
            return `昨天 ${getTime(newTime)}`;
        }
    } else {
        return getTime(newTime);
    }
}

function getWeek(value) {
    const weekDay = [
        '星期日',
        '星期一',
        '星期二',
        '星期三',
        '星期四',
        '星期五',
        '星期六'
    ];
    return weekDay[new Date(value).getDay()];
}

function cutTime(value, type = 'day', separator = '-') {
    /*
     **type = year 截取到年
     **type = month 截取到月
     **type = day 截取到日 默认
     **type = hours 截取到时
     **type = minutes 截取到分
     **type = normal 截取到秒
     **separator 年月日分隔符 默认 -
     */
    const time = new Date(value);
    switch (type) {
        case 'year':
            return time.getFullYear();
        case 'month':
            return `${time.getFullYear()}${separator}${leftPad(time.getMonth() + 1)}`;
        case 'day':
            return `${time.getFullYear()}${separator}${leftPad(time.getMonth() + 1)}${separator}${leftPad(time.getDate())}`;
        case 'hours':
            return `${time.getFullYear()}${separator}${leftPad(time.getMonth() + 1)}${separator}${leftPad(time.getDate())} ${leftPad(time.getHours())}`;
        case 'minutes':
            return `${time.getFullYear()}${separator}${leftPad(time.getMonth() + 1)}${separator}${leftPad(time.getDate())} ${leftPad(time.getHours())}:${leftPad(time.getMinutes())}`;
        default:
            return `${time.getFullYear()}${separator}${leftPad(time.getMonth() + 1)}${separator}${leftPad(time.getDate())} ${leftPad(time.getHours())}:${leftPad(time.getMinutes())}:${leftPad(time.getSeconds())}`;
    }
}

function getTime(value, type = 'minutes', pad = true) {
    /*
     **type = hours 获取到时
     **type = minutes 获取到分 默认
     **type = seconds 获取到秒
     **pad 是否补零，默认 true
     */
    const time = new Date(value);
    switch (type) {
        case 'hours':
            return pad ? leftPad(time.getHours()) : time.getHours();
        case 'seconds':
            return pad ? `${leftPad(time.getHours())}:${leftPad(time.getMinutes())}:${leftPad(time.getSeconds())}` : `${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
        default:
            return pad ? `${leftPad(time.getHours())}:${leftPad(time.getMinutes())}` : `${time.getHours()}:${time.getMinutes()}`;
    }
}
const getCountDownObj = (v) => {
    const getValue = (x) => {
        const value = Math.floor(v / x);
        v -= value * x;
        return value;
    };

    const h = leftPad(getValue(1000 * 60 * 60), 2);
    const m = leftPad(getValue(1000 * 60), 2);
    const s = leftPad(getValue(1000), 2);
    const ms = getValue(100);
    return { h, m, s, ms };
};

function leftPad(v) {
    if (v < 10) {
        return '0' + v;
    }
    return v;
}
export {
    getTime,
    difftime,
    chatTime,
    getWeek,
    cutTime,
    leftPad,
    getCountDownObj
};